//
//  MyConfigTool.m
//  MyConfigTool
//
//  Created by 李蔚 on 17/1/6.
//  Copyright © 2017年 陈进. All rights reserved.
//

#import "MyConfigTool.h"

@implementation MyConfigTool

+(UIImage *)createImageWithColor:(UIColor *)color{
    
    CGRect rect = CGRectMake(0.0f, 0.0f, 1.0f, 1.0f);
    UIGraphicsBeginImageContext(rect.size);
    CGContextRef context = UIGraphicsGetCurrentContext();
    CGContextSetFillColorWithColor(context, [color CGColor]);
    CGContextFillRect(context, rect);
    UIImage * theImg = UIGraphicsGetImageFromCurrentImageContext();
    UIGraphicsEndImageContext();
    return theImg;
}

+(NSString *)hideString:(NSString *)string WithHead:(NSInteger)headLong andTail:(NSInteger)tailLong{
    
    NSString * headerNum = [string substringToIndex:headLong];
    NSString * footerNum = [string substringFromIndex:[string length] - tailLong];
    NSInteger startCount = [string length] - (headLong + tailLong);
    NSString * tempStr = @"*";
    for (int i = 1; i < startCount; i++) {
        tempStr = [NSString stringWithFormat:@"%@*",tempStr];
    }
    return [NSString stringWithFormat:@"%@%@%@",headerNum,tempStr,footerNum];
}

+(CGSize)string:(NSString *)str sizeWithFont:(CGFloat)font{
    CGSize size = [str sizeWithAttributes:@{NSFontAttributeName:[UIFont systemFontOfSize:font]}];
    return size;
}
+(CGSize)stringnNew:(NSString *)str sizeWithFont:(CGFloat)font{
    font = font / 2.54;
    CGSize size = [str sizeWithAttributes:@{NSFontAttributeName:[UIFont systemFontOfSize:font]}];
    return size;
    
}

+(UIButton *)createButtonWithFrame:(CGRect)frame title:(NSString *)title titleColor:(UIColor *)color titleFont:(UIFont *)font target:(id)target action:(SEL)action image:(UIImage *)image backgroundImage:(UIImage *)backgroundImage selectedBackgroundImage:(UIImage *)selectedImage{
    
    UIButton * button = [UIButton buttonWithType:UIButtonTypeCustom];
    [button setFrame:frame];
    [button setTitle:title forState:UIControlStateNormal];
    [button setTitleColor:color forState:UIControlStateNormal];
    if (image != nil) {
        [button setImage:image forState:UIControlStateNormal];
    }
    if (backgroundImage != nil) {
        [button setBackgroundImage:backgroundImage forState:UIControlStateNormal];
    }
    if (selectedImage != nil) {
        [button setBackgroundImage:selectedImage forState:UIControlStateSelected];
    }
    button.titleLabel.font = font;
    [button addTarget:target action:action forControlEvents:UIControlEventTouchUpInside];
    return button;
    
}

+(UITextField *)createTextfieldWithFrame:(CGRect)frame backgroundImage:(UIImage *)image font:(UIFont *)font inView:(UIView *)view delegate:(id)delegete placeholder:(NSString *)placeholder{
    
    UITextField * textFild = [[UITextField alloc]initWithFrame:frame];
    textFild.background = image;
    textFild.font = font;
    textFild.delegate = delegete;
    textFild.placeholder = placeholder;
    [view addSubview:textFild];
    return textFild;
    
}
+(NSString *)string:(NSString *)str chooseStr:(NSString *)chooseStr isFrom:(NSInteger)isFrom{
    
    NSString * getStr = str;
    if (getStr.length !=0) {
        NSRange range =[getStr rangeOfString:chooseStr];
        if (isFrom == 0) {
            //TOstring
            getStr = [getStr substringToIndex:range.location];
        }else if (isFrom == 1){
            //FromString
            getStr = [getStr substringFromIndex:range.location + 1];
        }
    }
    
    return getStr;
    
}
+(UIImageView *)createImageViewWithFrame:(CGRect)frame imageName:(NSString *)imageName bgColor:(UIColor *)bgColor{
    
    UIImageView * imageView = [[UIImageView alloc]initWithFrame:frame];
    imageView.backgroundColor = bgColor;
    imageView.layer.cornerRadius = CGRectGetHeight(imageView.bounds) / 2;
    imageView.clipsToBounds = YES;
    imageView.image = [UIImage imageNamed:imageName];
    return imageView;
    
}

+(UIButton *)createButtonWithFrame:(CGRect)frame buttonTitle:(NSString *)buttonTitle titleColor:(UIColor *)titleColor btnTitleFont:(NSInteger)titleFont target:(id)target action:(SEL)action{
    
    UIButton * button = [UIButton buttonWithType:UIButtonTypeCustom];
    //    CGFloat btnW = [self string:buttonTitle sizeWithFont:titleFont].width;
    [button setFrame:frame];
    [button setTitle:buttonTitle forState:UIControlStateNormal];
    [button setTitleColor:titleColor forState:UIControlStateNormal];
    button.titleLabel.font = [UIFont systemFontOfSize:titleFont];
    [button addTarget:target action:action forControlEvents:UIControlEventTouchUpInside];
    
    return button;
}
+(UITextField *)createTextfieldWithFrame:(CGRect)frame placeholderTitle:(NSString *)placeHolderTitle{
    
    UITextField * textfield = [[UITextField alloc]initWithFrame:frame];
    //    textfield.borderStyle = UITextBorderStyleRoundedRect;
    textfield.placeholder = placeHolderTitle;
    return textfield;
    
}


+(UIColor *)stringTOColor:(NSString *)str
{
    if (!str || [str isEqualToString:@""]) {
        return nil;
    }
    unsigned red,green,blue;
    NSRange range;
    range.length = 2;
    range.location = 1;
    [[NSScanner scannerWithString:[str substringWithRange:range]] scanHexInt:&red];
    range.location = 3;
    [[NSScanner scannerWithString:[str substringWithRange:range]] scanHexInt:&green];
    range.location = 5;
    [[NSScanner scannerWithString:[str substringWithRange:range]] scanHexInt:&blue];
    UIColor *color= [UIColor colorWithRed:red/255.0f green:green/255.0f blue:blue/255.0f alpha:1];
    return color;
}


+(UIButton *)createButtonWithFrame:(CGRect)frame btnTitle:(NSString *)btnTitle titleFont:(UIFont *)titleFont target:(id)target action:(SEL)action view:(UIView *)view{
    
    UIButton * button = [self createButtonWithFrame:frame title:btnTitle titleColor:[self stringTOColor:@"#ffee02"] titleFont:titleFont target:target action:action image:nil backgroundImage:nil selectedBackgroundImage:nil];
    button.center = CGPointMake(view.center.x, button.center.y);
    button.backgroundColor = [UIColor clearColor];
    button.layer.cornerRadius = CGRectGetHeight(button.bounds) / 2;
    button.layer.borderColor = [self stringTOColor:@"#ffee02"].CGColor;
    button.layer.borderWidth = 1;
    
    return button;
    
}

+(UILabel *)createLabelWithFrame:(CGRect)frame labelTitle:(NSString *)labelTItle labelFont:(UIFont *)labelFont lableColor:(UIColor *)color{
    
    UILabel * label = [[UILabel alloc]initWithFrame:frame];
    label.text = labelTItle;
    label.textColor = color;
    label.font = labelFont;
    return label;
    
}

+(UILabel *)createAutoLabelWithFrame:(CGRect)frame labelTitle:(NSString *)labelTitle labelFont:(UIFont *)labelFont titleColor:(UIColor *)titleColor breakLine:(NSInteger)breakLine{
    
    UILabel * tempLabel = [[UILabel alloc]initWithFrame:frame];
    tempLabel.text = labelTitle;
    tempLabel.font = labelFont;
    tempLabel.textColor = titleColor;
    tempLabel.numberOfLines = breakLine;
    tempLabel.lineBreakMode = NSLineBreakByCharWrapping;
    return tempLabel;
    
}

+(UITextField *)createTextfieldWithColor:(UIColor *)placeColor placeFont:(UIFont *)placeFont{
    
    UITextField * tempTF = [[UITextField alloc]init];
    [tempTF setValue:placeColor forKeyPath:@"_placeholderLabel.textColor"];
    [tempTF setValue:placeFont forKeyPath:@"_placeholderLabel.font"];
    return tempTF;
    
}

+(NSString *)stampToDateWithStr:(NSString *)stampStr format:(NSString *)formatStr{
    
    NSString * dateStr ;
    NSDateFormatter * formatter = [[NSDateFormatter alloc]init];
    [formatter setDateFormat:formatStr];
    NSInteger stampLength = stampStr.length;
    long long valueStm = stampLength >= 13 ? ([stampStr longLongValue]) / 1000 : ([stampStr longLongValue]);
    NSDate * showDate = [NSDate dateWithTimeIntervalSince1970:valueStm + 28800];
    
    NSTimeZone * zone = [NSTimeZone systemTimeZone];
    NSDate *date = [NSDate date];
    NSInteger interval = [zone secondsFromGMTForDate:date];
    
    showDate = [showDate  dateByAddingTimeInterval:-interval];
    dateStr = [formatter stringFromDate:showDate];
    return dateStr;
    
}

+(NSString *)dateStrToStampStrWithDateStr:(NSString *)dateStr format:(NSString *)formatStr{
    
    NSString * stampStr ;
    NSDateFormatter * formatter = [[NSDateFormatter alloc]init];
    [formatter setDateFormat:formatStr];
    
    //------------将字符串按formatter转成nsdate
    NSDate* date = [formatter dateFromString:dateStr];
    stampStr = [NSString stringWithFormat:@"%ld", (long)[date timeIntervalSince1970]] ;
    return stampStr;

}


+(NSString *)compareStampWithStampValue:(NSString *)stampValue format:(NSString *)formatStr{
    
    NSString * returnStr;
    NSDate * currentDate = [NSDate date];
    //时间转时间戳
    NSString * currentStampStr = [NSString stringWithFormat:@"%1.0f",[currentDate timeIntervalSince1970]];
    
    long long stamp = [currentStampStr longLongValue] - [stampValue longLongValue];
    
    NSString * stampStr = [NSString stringWithFormat:@"%lld",stamp];
    returnStr =  [self stampToDateWithStr:stampStr format:formatStr];
    
    return  returnStr;
    
    
    
}




+(UIViewController *)ReturnViewControllerByView:(UIView *)view{
    
    id object = [view nextResponder];
    while (![object isKindOfClass:[UIViewController class]] && object != nil) {
        object = [object nextResponder];
    }
    UIViewController * superVC = (UIViewController *)object;
    return superVC;
    
}

+(NSString *)getDetailDateOfdateStr:(NSString *)dateStr{
    
    NSString * returnStr;
    //根据空格将月日 和 时分拆分
    NSArray * monthAndHourArr = [dateStr componentsSeparatedByString:@" "];
    //根据-将月日拆分
    NSArray * monthAndDayArr = [monthAndHourArr[0] componentsSeparatedByString:@"-"];
    //根据:将时分拆开
    NSArray * hourAndSecondArr = [monthAndHourArr[1] componentsSeparatedByString:@":"];
    if ([monthAndDayArr[0] integerValue] > 1) {
        returnStr = [NSString stringWithFormat:@"%ld月前",([monthAndDayArr[0] integerValue] - 1)];
        return returnStr;
        
    }else{
        
        if ([monthAndDayArr[1] integerValue] > 1) {
            returnStr = [NSString stringWithFormat:@"%ld天前",([monthAndDayArr[1] integerValue] - 1)];
            return returnStr;
        }else{
            
            if ([hourAndSecondArr[0] integerValue]  > 0) {
                returnStr = [NSString stringWithFormat:@"%ld小时前",[hourAndSecondArr[0] integerValue]];
                return returnStr;
                
            }else{
                if ([hourAndSecondArr[1] integerValue] > 0) {
                    returnStr = [NSString stringWithFormat:@"%ld分钟前",[hourAndSecondArr[1] integerValue]];
                    return returnStr;
                }else{
                    returnStr = @"刚刚";
                    return returnStr;
                }
                
            }
            
            
            
        }
        
    }
    return returnStr;
}

+ (NSString *)getUUID {
    CFUUIDRef cfuuid =CFUUIDCreate(kCFAllocatorDefault);
    NSString *cfuuidString =(NSString*)CFBridgingRelease(CFUUIDCreateString(kCFAllocatorDefault, cfuuid));
    return cfuuidString;
}

+ (CGRect)myConfigWithLabelTextRectWithSize:(CGSize )size withFontSize:(NSInteger )fontSize withText:(NSString *)text {
    CGRect frame = [text boundingRectWithSize:size options:NSStringDrawingUsesLineFragmentOrigin | NSStringDrawingUsesFontLeading  attributes:@{NSFontAttributeName : [UIFont systemFontOfSize:fontSize]} context:nil];
    return frame;
}

+(id)dictionaryWithJsonStr:(NSString *)jsonString{
    
    if (jsonString == nil) {
        return nil;
    }
    
    NSData *jsonData = [jsonString dataUsingEncoding:NSUTF8StringEncoding];
    NSError *err;
    id obj = [NSJSONSerialization JSONObjectWithData:jsonData
                                                        options:NSJSONReadingMutableContainers
                                                          error:&err];
    if(err) {
        NSLog(@"json解析失败：%@",err);
        return nil;
    }
    return obj;
    
}

+(NSString *)CheckStr:(NSString *)getStr andContailsStr:(NSString *)contansStr andIndex:(NSInteger)index{
    
    NSString * sendStr = getStr;
    NSArray * arr;
    if ([getStr containsString:contansStr]) {
        
        arr = [getStr componentsSeparatedByString:contansStr];
        sendStr = arr[index];
        
    }
    
    return sendStr;
}

//+ (void)stopRefresh:(UITableView *)tableView {
//    if ([tableView.mj_header isRefreshing]) {
//        [tableView.mj_header endRefreshing];
//    }
//    if ([tableView.mj_footer isRefreshing]) {
//        [tableView.mj_footer endRefreshing];
//    }
//}
//
//+(void)stopRefreshCollectionView:(UICollectionView *)collectionView {
//    if ([collectionView.mj_header isRefreshing]) {
//        [collectionView.mj_header endRefreshing];
//    }
//    if ([collectionView.mj_footer isRefreshing]) {
//        [collectionView.mj_footer endRefreshing];
//    }
//}
+(NSString *)stringReplaceSpaceStr:(NSString *)str{
    
    return [str stringByReplacingOccurrencesOfString:@" " withString:@""];
    
}


+(void)setBlurImage:(UIImageView *)imageView AlphaValue:(CGFloat)AlphaValue{
    
    UIBlurEffect *blurEffect = [UIBlurEffect effectWithStyle:UIBlurEffectStyleLight];
    //毛玻璃视图
    UIVisualEffectView *effectView = [[UIVisualEffectView alloc]initWithEffect:blurEffect];
    //添加到要有毛玻璃特效的控件中
    effectView.frame = imageView.bounds;
    //设置毛玻璃的透明度
    effectView.alpha = AlphaValue;
    [imageView addSubview:effectView];
    
}


+(void)GoToLoginVCWithViewController:(UIViewController *)VC{

//    NSString * loginStr = [USERDEFAULTS objectForKey:LOGIN_SUCESS];
//    if ([loginStr isEqualToString:@"1"]) {
//
//        [self showStatusWithMsg:@"此账号已经在其他设备登录" title:@"安全提示" AndDelegate:self];
//        [USERDEFAULTS removeObjectForKey:LOGIN_SUCESS];
//        CJTabbarViewController * tabbarVC = [[CJTabbarViewController alloc]init];
//        [VC presentViewController:tabbarVC animated:YES completion:nil];
//    }


}

+(CGFloat)ReturnWidthWithImgName:(NSString *)imageName imgType:(NSInteger)imgType{
    
    CGFloat imgH = [UIImage imageNamed:imageName].size.height;
    CGFloat imgW = [UIImage imageNamed:imageName].size.width;
    if (imgType == 0) {
        return imgW;
    }
    return imgH;
    
}

+(void)showStatusWithMsg:(NSString *)msg title:(NSString *)title AndDelegate:(id)delegate{
    
    UIAlertView * alertView = [[UIAlertView alloc]initWithTitle:title message:msg delegate:delegate cancelButtonTitle:@"确定" otherButtonTitles:nil, nil];
    [alertView show];
    
}

/********************17-2-22新增***********************/

+ (NSString *)jsonStringWithParameters:(id)parameters {
    if (parameters == nil) {
        return @"";
    }
    NSData *data = [NSJSONSerialization dataWithJSONObject:parameters options:NSJSONWritingPrettyPrinted error:nil];
    return [[NSString alloc]initWithData:data encoding:NSUTF8StringEncoding];
}


+ (BOOL)isTextEmpty:(NSString *)string {
    if (string == nil || (id)string == [NSNull null] || [string isEqualToString:@"(null)"]||[string isEqualToString:@"<null>"]) {
        return YES;
    }else{
        if (![string respondsToSelector:@selector(length)]) {
            return YES;
        }
        string = [string stringByTrimmingCharactersInSet:[NSCharacterSet whitespaceAndNewlineCharacterSet]];
        if ([string length]) {
            return NO;
        }
    }
    return YES;
}


+(CGFloat)getString:(NSString *)string rowWidth:(CGFloat)width columnHeight:(CGFloat)height fontSize:(CGFloat)font{

    //单行的宽
    CGFloat rowW = width;
    //所有字体的宽
    CGFloat allW = [self string:string sizeWithFont:font].width;
    CGFloat colums = ceil(allW * 1.0/rowW * 1.0);
    CGFloat rowH = height * colums;
    return rowH;
    
}

+(NSString *)returnStrByGetSubArr:(NSArray *)arr centerStr:(NSString *)centerStr byStr:(NSString *)str{
    NSString * id_str;
    for (NSDictionary * dict in arr) {
        if (id_str.length == 0) {
            id_str = [NSString stringWithFormat:@"%@",dict[str]];
        }else{
            id_str = [NSString stringWithFormat:@"%@%@%@",id_str,centerStr,dict[str]];
        }
    }
    return id_str;
}


+ (void)stopRefresh:(UITableView *)tableView {
//    if ([tableView.mj_header isRefreshing]) {
//        [tableView.mj_header endRefreshing];
//    }
//    if ([tableView.mj_footer isRefreshing]) {
//        [tableView.mj_footer endRefreshing];
//    }
}

+(UIWindow *)lastWindow
{
//    NSString * version = Current_Version;
//    if (version.doubleValue >= 11.0) {
//         return [[[UIApplication sharedApplication] delegate] window];
//    }else{
//        NSArray *windows = [UIApplication sharedApplication].windows;
//        for(UIWindow *window in [windows reverseObjectEnumerator]) {
//
//            if ([window isKindOfClass:[UIWindow class]] &&
//                CGRectEqualToRect(window.bounds, [UIScreen mainScreen].bounds))
//
//                return window;
//        }
    
        return [UIApplication sharedApplication].keyWindow;
//    }
    
}


+(CGSize)returnImgSizeWithImgName:(NSString *)imgName{

    UIImage *image = [UIImage imageNamed:imgName];
    CGImageRef imageRef = [image CGImage];
    CGSize size = CGSizeMake(CGImageGetWidth(imageRef), CGImageGetHeight(imageRef));//这个size就是实际图片的像素大小
    return size;
}


+(NSMutableAttributedString *)returnLineOfCenterWithOriginalStr:(NSString *)originalStr{

    //中划线
    NSDictionary *lineDic = @{NSStrikethroughStyleAttributeName:[NSNumber numberWithInteger:NSUnderlineStyleSingle]};
    NSMutableAttributedString * lineStr = [[NSMutableAttributedString alloc]initWithString:originalStr attributes:lineDic];
    return lineStr;
}

+(NSString *)numChangeToStrWithNum:(id)num{
    
    NSString * str = [NSString stringWithFormat:@"%@",num];
    return str;
    
}

+(NSString *)returnCurrentTimeDateWithDateStr:(NSString *)dateStr{
    
    NSString * currentStr = nil;
    
    NSDateFormatter *formatter = [[NSDateFormatter alloc] init];
    
    // ----------设置你想要的格式,hh与HH的区别:分别表示12小时制,24小时制
    
    [formatter setDateFormat:dateStr];
    
    //现在时间,你可以输出来看下是什么格式
    
    NSDate *datenow = [NSDate date];
    
    //----------将nsdate按formatter格式转成nsstring
    
    currentStr = [formatter stringFromDate:datenow];
    
    return currentStr;
}

+ (CGFloat)folderSizeAtPath:(NSString *)folderPath
{
    NSFileManager *manager = [NSFileManager defaultManager];
    if (![manager fileExistsAtPath:folderPath]) {
        return 0;
    }
    
    NSEnumerator *childFilesEnumerator = [[manager subpathsAtPath:folderPath] objectEnumerator];
    
    NSString *fileName = nil;
    long long folderSize = 0;
    while ((fileName = [childFilesEnumerator nextObject]) != nil) {
        NSString *fileAbsolutePath = [folderPath stringByAppendingPathComponent:fileName];
        folderSize += [self fileSizeAtPath:fileAbsolutePath];
    }
    return folderSize/(1024.0*1024.0);
}

+ (long long)fileSizeAtPath:(NSString *)filePath
{
    NSFileManager* manager = [NSFileManager defaultManager];
    if ([manager fileExistsAtPath:filePath]){
        return [[manager attributesOfItemAtPath:filePath error:nil] fileSize];
    }
    return 0;
    
}

+(void)callSbWithPhoneStr:(NSString *)PhoneStr{
    
    NSMutableString* str=[[NSMutableString alloc] initWithFormat:@"telprompt://%@",PhoneStr];
    // NSLog(@"str======%@",str);
    [[UIApplication sharedApplication] openURL:[NSURL URLWithString:str]];
}

+ (NSString *)transform:(NSString *)chinese{
    //将NSString装换成NSMutableString
    NSMutableString *pinyin = [chinese mutableCopy];
    
    //将汉字转换为拼音(带音标)
    CFStringTransform((__bridge CFMutableStringRef)pinyin, NULL, kCFStringTransformMandarinLatin, NO);
    NSLog(@"%@", pinyin);
    
    //去掉拼音的音标
    CFStringTransform((__bridge CFMutableStringRef)pinyin, NULL, kCFStringTransformStripCombiningMarks, NO);
    NSLog(@"%@", pinyin);
    
    //返回最近结果
    return pinyin;
    
}


#pragma mark 判断银行卡号是否合法
+ (BOOL)checkBankCardNumber:(NSString *)cardNumber
{
    int oddSum = 0;     // 奇数和
    int evenSum = 0;    // 偶数和
    int allSum = 0;     // 总和
    
    // 循环加和
    for (NSInteger i = 1; i <= cardNumber.length; i++)
    {
        NSString *theNumber = [cardNumber substringWithRange:NSMakeRange(cardNumber.length-i, 1)];
        int lastNumber = [theNumber intValue];
        if (i%2 == 0)
        {
            // 偶数位
            lastNumber *= 2;
            if (lastNumber > 9)
            {
                lastNumber -=9;
            }
            evenSum += lastNumber;
        }
        else
        {
            // 奇数位
            oddSum += lastNumber;
        }
    }
    allSum = oddSum + evenSum;
    // 是否合法
    if (allSum%10 == 0)
    {
        return YES;
    }
    else
    {
        return NO;
    }
}

+(UIImage *)captureCurrentView:(UIView *)view {
    CGRect frame = view.frame;
//    UIGraphicsBeginImageContext(frame.size);
    UIGraphicsBeginImageContextWithOptions(frame.size, NO, 0.0);
    CGContextRef contextRef = UIGraphicsGetCurrentContext();
    [view.layer renderInContext:contextRef];
    UIImage *image = UIGraphicsGetImageFromCurrentImageContext();
    UIGraphicsEndImageContext();
    return image;
}

+ (void)myConfigMaxStarwordsLengthWithTextField:(UITextField *)textField withMaxSL:(NSInteger )maxSL {
    NSString *toBeString = textField.text;
    
    //获取高亮部分
    UITextRange *selectedRange = [textField markedTextRange];
    UITextPosition *position = [textField positionFromPosition:selectedRange.start offset:0];
    
    // 没有高亮选择的字，则对已输入的文字进行字数统计和限制
    if (!position)
    {
        if (toBeString.length > maxSL)
        {
            NSRange rangeIndex = [toBeString rangeOfComposedCharacterSequenceAtIndex:maxSL];
            if (rangeIndex.length == 1)
            {
                textField.text = [toBeString substringToIndex:maxSL];
            }
            else
            {
                NSRange rangeRange = [toBeString rangeOfComposedCharacterSequencesForRange:NSMakeRange(0, maxSL)];
                textField.text = [toBeString substringWithRange:rangeRange];
            }
        }
    }
    
}

+ (void)myConfigMaxStarwordsLengthWithTextView:(UITextView *)textView withMaxSL:(NSInteger )maxSL {
    NSString *toBeString = textView.text;
    
    //获取高亮部分
    UITextRange *selectedRange = [textView markedTextRange];
    UITextPosition *position = [textView positionFromPosition:selectedRange.start offset:0];
    
    // 没有高亮选择的字，则对已输入的文字进行字数统计和限制
    if (!position)
    {
        if (toBeString.length > maxSL)
        {
            NSRange rangeIndex = [toBeString rangeOfComposedCharacterSequenceAtIndex:maxSL];
            if (rangeIndex.length == 1)
            {
                textView.text = [toBeString substringToIndex:maxSL];
            }
            else
            {
                NSRange rangeRange = [toBeString rangeOfComposedCharacterSequencesForRange:NSMakeRange(0, maxSL)];
                textView.text = [toBeString substringWithRange:rangeRange];
            }
        }
    }
}


//获取拼音首字母(传入汉字字符串, 返回大写拼音首字母)
+(NSString *)firstCharactor:(NSString *)aString
{
    //转成了可变字符串
    NSMutableString *str = [NSMutableString stringWithString:aString];
    //先转换为带声调的拼音
    CFStringTransform((CFMutableStringRef)str,NULL, kCFStringTransformMandarinLatin,NO);
    //再转换为不带声调的拼音
    CFStringTransform((CFMutableStringRef)str,NULL, kCFStringTransformStripDiacritics,NO);
    //转化为大写拼音
    NSString *pinYin = [str capitalizedString];
    //获取并返回首字母
    return [pinYin substringToIndex:1];
}


+ (UIImageView *)getLineViewInNavigationBar:(UIView *)view
{
    if ([view isKindOfClass:UIImageView.class] && view.bounds.size.height <= 1.0) {
        return (UIImageView *)view;
    }
    
    for (UIView *subview in view.subviews) {
        UIImageView *imageView = [self getLineViewInNavigationBar:subview];
        if (imageView) {
            return imageView;
        }
    }
    return nil;
}

+ (void)webViewWithJS:(UIWebView *)webView {
   NSString *javascript = [NSString stringWithFormat:@"var viewPortTag=document.createElement('meta');  \
                  viewPortTag.id='viewport';  \
                  viewPortTag.name = 'viewport';  \
                  viewPortTag.content = 'width=%d; initial-scale=1.0; maximum-scale=1.0; user-scalable=0;';  \
                  document.getElementsByTagName('head')[0].appendChild(viewPortTag);" , (int)webView.bounds.size.width];
    
    [webView stringByEvaluatingJavaScriptFromString:javascript];

}

@end
